#include "cc.h"
#include "bsp_clk.h"
#include "bsp_led.h"
#include "bsp_delay.h"
#include "bsp_beep.h"
#include "bsp_gpio.h"
#include "bsp_key.h"
#include "bsp_int.h"
#include "bsp_exti.h"
#include "bsp_epittimer.h"
#include "bsp_keyfilter.h"
#include "bsp_delay.h"
#include "bsp_uart.h"
#include "stdio.h"
#include "bsp_lcd.h"
#include "bsp_lcdapi.h"
#include "bsp_rtc.h"


char *banner = 	"========================================================\r\n"
				"正点原子I.MX6ULL ALPHA/Mini开发板Linux驱动之ARM裸机开发\r\n" \
				"--Date: 	%s\r\n" \
				"--Author: 	ChenHaoxu, Dimon.chen, 11813202388@qq.com\r\n" \
				"========================================================\r\n";



int main(void)
{

	static uint8_t led_state = OFF;
	// static uint8_t beep_state = OFF;

	int_init();			/* 中断初始化 */
	imx6u_clkinit();	/* 时钟主频初始化，PLL1, PLL2, PLL3 */
	clk_init();			/* 使能所有外设时钟 */
	led_init();			/* led gpio 初始化 */
	beep_init();		/* beep gpio 初始化 */
	//key_init();			/* key gpio 初始化 */
	exti_init();		/* gpio外设中断初始化 */

	//epittimer_init(0, 0, 33000000);	/* EPIT分频frac=0 1分频，EPIT1时钟源66MHz，EPIT1->LR加载值计数器=33MHz，定时周期为500ms */
	//epittimer_init(1, 0, 66000000/20);	/* EPIT分频frac=0 1分频，EPIT1时钟源66MHz，EPIT1->LR加载值计数器=33MHz，定时周期为1000/20=50ms */

	keyfilter_init();
	delay_init();
	uart_init();		/* UART初始化 */
	//lcd_init();			/* LCD 初始化 */
	


	led_switch(LED_0, ON);


	beep_switch(ON);
	delay(200);
	beep_switch(OFF);
	delay(200);
	beep_switch(ON);
	delay(200);
	beep_switch(OFF);

	printf(banner, __DATE__);
	printf("Hello World\r\n");

	lcd_init();			/* LCD 初始化 */

	rtc_init();

	int lcd_color[5] = {
		LCD_BLUE,
		LCD_GREEN,
		LCD_RED,
		LCD_CYAN,
		LCD_YELLOW
	};

	int i = 0;
	int hcnt = 0;
	int vcnt = 0;
	vcnt = 0;
	hcnt = 0;

	/* LCD屏幕上绘制矩形框 */
	for(i=0; ; i++){
		if(vcnt+300 >= tftlcd_dev.height)
			break;
		
		if(hcnt+200 >= tftlcd_dev.width)
			break;
		
		lcd_fill(hcnt, vcnt, 200 + hcnt, 300 + vcnt, lcd_color[i%5]);

		vcnt += 20;
		hcnt += 60;
	}

	/* LCD屏幕上打印字符串 */
	for(i=0; i< 10; i++){
		lcd_show_string(100, (100 + i * 32), 24*32 , 32, 24, "Hello World, I.MX6ULL");
	}
	
	/* LCD屏幕上绘制直线 */
	int cnt = 20;
	for(i=0; ; i++){
		if(cnt > tftlcd_dev.height)
			break;
		lcd_drawline(0,0, 600, cnt);
		cnt += 20;
	}


	struct rtc_datetime datetime;
	
    while(1){
		led_state = !led_state;
		led_switch(LED_0, led_state);
		//beep_switch(led_state);

		delay_ms(1000);

		rtc_getdatetime(&datetime);

		printf("当前时间: %d/%d/%d %d:%d:%d\r\n", datetime.year, datetime.month, datetime.day,
				datetime.hour, datetime.minute, datetime.second);
    }

    return 0;
}